package com.xinxing.learning.statemachine.config;

import com.xinxing.learning.statemachine.constants.CommonConstants;
import com.xinxing.learning.statemachine.entity.Order;
import com.xinxing.learning.statemachine.enums.OrderStatus;
import com.xinxing.learning.statemachine.enums.OrderStatusChangeEvent;
import com.xinxing.learning.statemachine.mapper.OrderMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import javax.annotation.Resource;
import java.util.EnumSet;


@Configuration
@EnableStateMachine(name = "orderStateMachine")
public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<OrderStatus, OrderStatusChangeEvent> {
    /**
     * 配置状态
     *
     * @param states
     * @throws Exception
     */
    @Override
    public void configure(StateMachineStateConfigurer<OrderStatus, OrderStatusChangeEvent> states) throws Exception {
        states
                .withStates()
                .initial(OrderStatus.WAIT_PAYMENT)
                .states(EnumSet.allOf(OrderStatus.class));
    }

    @Resource
    private OrderMapper orderMapper;
    /**
     * 配置状态转换事件关系
     *
     * @param transitions
     * @throws Exception
     */
    @Override
    public void configure(StateMachineTransitionConfigurer<OrderStatus, OrderStatusChangeEvent> transitions) throws Exception {
        transitions
                //支付事件:待支付-》待发货
                .withExternal().source(OrderStatus.WAIT_PAYMENT).target(OrderStatus.WAIT_DELIVER)
                // .action(new Action<OrderStatus, OrderStatusChangeEvent>() {
                //     @Override
                //     public void execute(StateContext<OrderStatus, OrderStatusChangeEvent> stateContext) {
                //         Message<OrderStatusChangeEvent> message = stateContext.getMessage();
                //         Order order = (Order) message.getHeaders().get(CommonConstants.orderHeader);
                //
                //         order.setStatus(OrderStatus.WAIT_DELIVER.getKey());
                //         orderMapper.updateById(order);
                //         stateContext.getStateMachine().getExtendedState().getVariables().put(CommonConstants.payTransition,1);
                //     }
                // })
                .event(OrderStatusChangeEvent.PAYED)
                .and()
                //发货事件:待发货-》待收货
                .withExternal().source(OrderStatus.WAIT_DELIVER).target(OrderStatus.WAIT_RECEIVE)
                .event(OrderStatusChangeEvent.DELIVERY)
                .and()
                //收货事件:待收货-》已完成
                .withExternal().source(OrderStatus.WAIT_RECEIVE).target(OrderStatus.FINISH)
                .event(OrderStatusChangeEvent.RECEIVED);
    }
}